home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / fileutils_3_3.lha / fileutils-3.3 / lib / backupfile.c < prev    next >
C/C++ Source or Header  |  1992-08-02  |  6KB  |  241 lines

  1. /* backupfile.c -- make Emacs style backup file names
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie <djm@ai.mit.edu>.
  19.    Some algorithms adapted from GNU Emacs. */
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <sys/types.h>
  24. #include "backupfile.h"
  25. #if defined(USG) || defined(STDC_HEADERS)
  26. #include <string.h>
  27. #define index strchr
  28. #define rindex strrchr
  29. #else
  30. #include <strings.h>
  31. #endif
  32.  
  33. #ifdef DIRENT
  34. #include <dirent.h>
  35. #ifdef direct
  36. #undef direct
  37. #endif
  38. #define direct dirent
  39. #define NLENGTH(direct) (strlen((direct)->d_name))
  40. #else /* !DIRENT */
  41. #define NLENGTH(direct) ((direct)->d_namlen)
  42. #ifdef USG
  43. #ifdef SYSNDIR
  44. #include <sys/ndir.h>
  45. #else /* !SYSNDIR */
  46. #include <ndir.h>
  47. #endif /* !SYSNDIR */
  48. #else /* !USG */
  49. #include <sys/dir.h>
  50. #endif /* !USG */
  51. #endif /* !DIRENT */
  52.  
  53. #ifdef VOID_CLOSEDIR
  54. /* Fake a return value. */
  55. #define CLOSEDIR(d) (closedir (d), 0)
  56. #else
  57. #define CLOSEDIR(d) closedir (d)
  58. #endif
  59.  
  60. #ifdef STDC_HEADERS
  61. #include <stdlib.h>
  62. #else
  63. char *malloc ();
  64. #endif
  65.  
  66. #ifndef isascii
  67. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  68. #else
  69. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  70. #endif
  71.  
  72. #if defined (HAVE_UNISTD_H)
  73. #include <unistd.h>
  74. #endif
  75.  
  76. #if defined (_POSIX_VERSION)
  77. /* POSIX does not require that the d_ino field be present, and some
  78.    systems do not provide it. */
  79. #define REAL_DIR_ENTRY(dp) 1
  80. #else
  81. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  82. #endif
  83.  
  84. /* Which type of backup file names are generated. */
  85. enum backup_type backup_type = none;
  86.  
  87. /* The extension added to file names to produce a simple (as opposed
  88.    to numbered) backup file name. */
  89. #ifdef AMIGA
  90. char *simple_backup_suffix = "!";
  91. #else
  92. char *simple_backup_suffix = "~";
  93. #endif
  94.  
  95. char *basename ();
  96. char *dirname ();
  97. static char *concat ();
  98. char *find_backup_file_name ();
  99. static char *make_version_name ();
  100. static int max_backup_version ();
  101. static int version_number ();
  102.  
  103. /* Return the name of the new backup file for file FILE,
  104.    allocated with malloc.  Return 0 if out of memory.
  105.    FILE must not end with a '/' unless it is the root directory.
  106.    Do not call this function if backup_type == none. */
  107.  
  108. char *
  109. find_backup_file_name (file)
  110.      char *file;
  111. {
  112.   char *dir;
  113.   char *base_versions;
  114.   int highest_backup;
  115.  
  116.   if (backup_type == simple)
  117.     return concat (file, simple_backup_suffix);
  118. #ifdef AMIGA
  119.   base_versions = concat (basename (file), ".!");
  120. #else
  121.   base_versions = concat (basename (file), ".~");
  122. #endif
  123.   if (base_versions == 0)
  124.     return 0;
  125.   dir = dirname (file);
  126.   if (dir == 0)
  127.     {
  128.       free (base_versions);
  129.       return 0;
  130.     }
  131.   highest_backup = max_backup_version (base_versions, dir);
  132.   free (base_versions);
  133.   free (dir);
  134.   if (backup_type == numbered_existing && highest_backup == 0)
  135.     return concat (file, simple_backup_suffix);
  136.   return make_version_name (file, highest_backup + 1);
  137. }
  138.  
  139. /* Return the number of the highest-numbered backup file for file
  140.    FILE in directory DIR.  If there are no numbered backups
  141.    of FILE in DIR, or an error occurs reading DIR, return 0.
  142.    FILE should already have ".~" appended to it. */
  143.  
  144. static int
  145. max_backup_version (file, dir)
  146.      char *file, *dir;
  147. {
  148.   DIR *dirp;
  149.   struct direct *dp;
  150.   int highest_version;
  151.   int this_version;
  152.   int file_name_length;
  153.   
  154.   dirp = opendir (dir);
  155.   if (!dirp)
  156.     return 0;
  157.   
  158.   highest_version = 0;
  159.   file_name_length = strlen (file);
  160.  
  161.   while ((dp = readdir (dirp)) != 0)
  162.     {
  163.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
  164.     continue;
  165.       
  166.       this_version = version_number (file, dp->d_name, file_name_length);
  167.       if (this_version > highest_version)
  168.     highest_version = this_version;
  169.     }
  170.   if (CLOSEDIR (dirp))
  171.     return 0;
  172.   return highest_version;
  173. }
  174.  
  175. /* Return a string, allocated with malloc, containing
  176.    "FILE.~VERSION~".  Return 0 if out of memory. */
  177.  
  178. static char *
  179. make_version_name (file, version)
  180.      char *file;
  181.      int version;
  182. {
  183.   char *backup_name;
  184.  
  185.   backup_name = malloc (strlen (file) + 16);
  186.   if (backup_name == 0)
  187.     return 0;
  188. #ifdef AMIGA
  189.   sprintf (backup_name, "%s.!%d!", file, version);
  190. #else
  191.   sprintf (backup_name, "%s.~%d~", file, version);
  192. #endif
  193.   return backup_name;
  194. }
  195.  
  196. /* If BACKUP is a numbered backup of BASE, return its version number;
  197.    otherwise return 0.  BASE_LENGTH is the length of BASE.
  198.    BASE should already have ".~" appended to it. */
  199.  
  200. static int
  201. version_number (base, backup, base_length)
  202.      char *base;
  203.      char *backup;
  204.      int base_length;
  205. {
  206.   int version;
  207.   char *p;
  208.   
  209.   version = 0;
  210.   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
  211.     {
  212.       for (p = &backup[base_length]; ISDIGIT (*p); ++p)
  213.     version = version * 10 + *p - '0';
  214. #ifdef AMIGA
  215.       if (p[0] != '!' || p[1])
  216. #else
  217.       if (p[0] != '~' || p[1])
  218. #endif
  219.     version = 0;
  220.     }
  221.   return version;
  222. }
  223.  
  224. /* Return the newly-allocated concatenation of STR1 and STR2.
  225.    If out of memory, return 0. */
  226.  
  227. static char *
  228. concat (str1, str2)
  229.      char *str1, *str2;
  230. {
  231.   char *newstr;
  232.   char str1_length = strlen (str1);
  233.  
  234.   newstr = malloc (str1_length + strlen (str2) + 1);
  235.   if (newstr == 0)
  236.     return 0;
  237.   strcpy (newstr, str1);
  238.   strcpy (newstr + str1_length, str2);
  239.   return newstr;
  240. }
  241.